﻿#!/usr/bin/python
# -*- encoding: utf-8 -*-
'''
@File    : FileDownloadWidget.py
@Desc    : 
@Version : 1.0
@Author  : echohelper 
@Email   : echohelper@qq.com
@WebSite : www.github.com/echohelper
@Time    : 2021/09/24 09:19:47
'''
# Start typing your code from here

from PyQt5 import QtCore, QtGui, QtWidgets

from module.demo.Ui_FileDownloadWidget import Ui_Form
from module.network.http.HttpDownloadFile import HttpDownloadFile


class FileDownloadWidget(QtWidgets.QWidget):

    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.initUi()

    def initUi(self) -> None:
        self.ui.lineEditUrl.setPlaceholderText('请输入下载的HTTP地址')
        self.ui.lineEditFolder.setPlaceholderText('请输入保存到的文件夹')
        self.ui.lineEditUrl.setText('https://dldir1.qq.com/qqfile/qq/PCTIM/TIM3.3.8/TIM3.3.8.22043.exe')
        self.ui.lineEditFolder.setText('f:/download')

    def onBtnSelectFolder(self, checked:bool) -> None:
        fileFolder = QtWidgets.QFileDialog.getExistingDirectory(None, 'Select Folder', '/')
        self.ui.lineEditFolder.setText(fileFolder)
        self.ui.textBrowser.append('select folder: ' + fileFolder)

    def onBtnDownload(self, checked:bool) -> None:
        print('download start')
        self.ui.textBrowser.append('download start')
        fileUrl = self.ui.lineEditUrl.text()
        fileFolder = self.ui.lineEditFolder.text() + '/'
        if fileUrl == '':
            self.ui.textBrowser.append('请输入下载地址')
            return;
        if fileFolder == '':
            self.ui.textBrowser.append('请输入保存到的文件夹')
            return;

        fullname = '111222'
        idx = fileUrl.rfind('/', 0, len(fileUrl))
        if idx != -1 :
            fullname = fileUrl[idx + 1:]

        retFileFolder = HttpDownloadFile().DownloadFile(fileUrl, fileFolder, fullname)
        if retFileFolder == '':
            self.ui.textBrowser.append('下载失败')
        else:
            self.ui.textBrowser.append('下载成功')
    
    def onBtnClear(self, checked:bool) -> None:
        self.ui.textBrowser.clear()
